<?php
namespace ThirdLogin;
/**
 *
 */
class weixin extends Login
{

    private $appid = '';
    private $appkey = '';
    private $token = '';
    private $openid = '';
    private $state = '';

    function __construct($config)
    {
        foreach ($config as $key => $value) {
            $this->$key = $value;
        }
    }

    public function view()
    {
        $this->getCode();

        return true;
    }


    //获取token
    public function getOpenid()
    {
        $code = input('get.code');
        // $state = input('get.state');
        $state = $this->State();
//        if (!$this->State($state)) {
//            $this->getError("验证state出错!");
//            return false;
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appid}&secret={$this->appkey}&code={$code}&grant_type=authorization_code";


        $res = $this->get_contents($url);

        $res = json_decode($res, true);
        if(array_key_exists('errcode', $res)){
            return false;
        }
        //此处如果正确的话,会返回6个信息。access_token是有时效性的。在后续的一些接口中，会使用到。但目前登陆获取用户信息仅是在很短的时间内完成。、
        //所以目前只是用大S方法缓存下
//        if (!$res['openid']) {
//            $this->getError("获取token失败!");
//            return false;
//        }
        //session('res', $res, $res['expires_in']);
        $this->token = $res['access_token'];
        $this->openid = $res['openid'];
        return $res['openid'];
    }

    public function getUserInfo($openid = null)
    {

        if (!$openid) {
            $openid = $this->openid;
        }
        $token = $this->token;
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$token}&openid={$openid}";
        $res = $this->get_contents($url);
        $res = json_decode($res, true);

        $res = $this->create($res);
        return $res;
    }

    //设置回调地址
    public function setCallback($callback)
    {
        $this->callback = $callback[0];
        return true;
    }

    public function create($info)
    {
        $res['nickname'] = $info['nickname'];
        $res['sex'] = $info['sex'];
        $res['unionid'] = $info['unionid'];
        $res['city'] = $info['city'];
        $res['province'] = $info['province'];
        $res['headimgurl1'] = $info['headimgurl'];
        $res['headimgurl2'] = $info['headimgurl'];
        return $res;
    }


    private function State($state = null)
    {
        //sam add
        return  $this->state?$this->state:'';
        //
        if (!$state) {
            $state = md5(uniqid(rand(), TRUE));
            session("third_login_state", $state);
        } else {
            $s = session("third_login_state");
           session("third_login_state", null);

            if ($state != $s) {
                return false;
            }
            return true;
        }
        return $state;
    }


    private function getCode()
    {
        $callback = UrlEncode($this->callback);
        $state = $this->State();
        $url = "https://open.weixin.qq.com/connect/qrconnect?appid={$this->appid}&redirect_uri={$callback}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect";

        header("Location:{$url}");   
        exit;
    }
    public function getUrl(){
        $callback = UrlEncode($this->callback);
        $state = $this->State();
        $url = "https://open.weixin.qq.com/connect/qrconnect?appid={$this->appid}&redirect_uri={$callback}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect";
        return $url;
    }

}
